﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E.Web.UI.Page;
using X.Common;
using E.Enum;
using E.BLL;
using E.Model;

namespace E.Web.training.exam
{
    public partial class TestSelf : BasePage
    {
        public int examid;
        public bool err = false;
        public int categoryid;

        protected override void ShowPage()
        {
            string reffer = XRequest.GetUrlReferrer();
            examid = XRequest.GetInt("examid", -1);
            categoryid = XRequest.GetInt("cid", -1);
            //examid = 1;


            if (examid != -1 && categoryid != -1)
            {
                bool r = false;
                switch ((EnumExamCategory)categoryid)
                {
                    case EnumExamCategory.CourseExam:
                        r = true;
                        break;
                    case EnumExamCategory.CertificateExam:
                        r = true;
                        break;
                    default: break;
                }
                if (r)
                {
                    ExamBLL bll = new ExamBLL();

                    int existExamid = bll.GetTestInfo(userid, examid, (EnumExamCategory)categoryid);

                    if (existExamid == -1)
                    {
                        //Utils.WriteCookie("examid", examid.ToString());
                        //Utils.WriteCookie("cid", categoryid.ToString());
                    }
                    else
                    {
                        examid = existExamid;

                        ShortExamInfoModel model = bll.GetShortExamInfo(examid);

                        DateTime now = DateTime.Now;
                        if (now >= model.StartTime && now < model.EndTime)
                        {
                            ShortTestModel testInfo = bll.GetUserTestStartTime(examid, userid);
                            if (testInfo.ID != -1)
                            {
                                if (!testInfo.IsOver)
                                {
                                    if (model.ID != -1)
                                    {
                                        now = DateTime.Now;
                                        int timeSpan = (now - testInfo.StartTime).Minutes;
                                        if (timeSpan >= model.ExamTime)
                                        {
                                            TestModel testModel = new TestModel();
                                            testModel.ExamID = examid;
                                            testModel.UserID = userid;
                                            testModel.AnswerArray = "";
                                            testModel.Score = 0;
                                            testModel.SubmitTime = now;
                                            (new ExamBLL()).UpdateTestInfo(testModel);
                                            err = true;
                                            ExecuteJS("alert('考试时间结束。');");
                                        }
                                        else
                                        {
                                            //Utils.WriteCookie("examid", examid.ToString());
                                            //Utils.WriteCookie("cid", categoryid.ToString());
                                        }
                                    }
                                    else
                                    {
                                        err = true;
                                        ExecuteJS("alert('提交错误，请稍后再试。');");
                                    }
                                }
                                else
                                {
                                    err = true;
                                    ExecuteJS("alert('已经参加过该考试');");
                                }
                            }
                            else
                            {
                                //Utils.WriteCookie("examid", examid.ToString());
                                //Utils.WriteCookie("cid", categoryid.ToString());
                            }
                        }
                        else
                        {
                            err = true;
                            ExecuteJS("alert('考试未开始或已经结束');");
                        }
                    }
                }
                else
                {
                    err = true;
                    ExecuteJS("alert('提交错误，请稍后再试。');");
                }
            }
            else
            {
                err = true;
                ExecuteJS("alert('服务器忙，请稍后再试。');");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}